package com.example.casper.Experiment2024.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.ThreadUtils;
import com.example.casper.Experiment2024.R;
import com.example.casper.Experiment2024.widget.GameSurfaceView;

public class GameViewFragment extends Fragment {

    private GameSurfaceView gameSurfaceView;
    private TextView bombCountText;
    private TextView timeText;
    private Handler handler = new Handler();
    private Runnable updateRunnable;
    private long startTime;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_game_view, container, false);

        gameSurfaceView = view.findViewById(R.id.gameView);
        bombCountText = view.findViewById(R.id.bombCount);
        timeText = view.findViewById(R.id.time);

        gameSurfaceView.setBombCountListener(new GameSurfaceView.BombCountListener() {
            @Override
            public void onBombCountUpdate(int count) {
                ThreadUtils.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        bombCountText.setText("Bombs Dodged: " + count);
                    }
                });
            }
        });

        updateRunnable = new Runnable() {
            @Override
            public void run() {
                if (startTime != 0) {
                    long elapsedTime = System.currentTimeMillis() - startTime;
                    timeText.setText("Time Survived: " + formatTime(elapsedTime));
                }
                handler.postDelayed(this, 1000); // Update every second
            }
        };

        gameSurfaceView.setGameOverListener(new GameSurfaceView.GameOverListener() {
            @Override
            public void onGameOver() {
                handler.removeCallbacks(updateRunnable);
                startTime = 0; // Reset start time
                ThreadUtils.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getContext(), "Game Over!", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        gameSurfaceView.resume(); // Start the game
        startTime = System.currentTimeMillis(); // Start the timer
        handler.post(updateRunnable); // Start updating the time

        return view;
    }

    @Override
    public void onPause() {
        super.onPause();
        gameSurfaceView.pause(); // Pause the game when the fragment is paused
    }

    @Override
    public void onResume() {
        super.onResume();
        gameSurfaceView.resume(); // Resume the game when the fragment is resumed
    }

    private String formatTime(long millis) {
        long minutes = millis / 60000;
        long seconds = (millis % 60000) / 1000;
        return String.format("%02d:%02d", minutes, seconds);
    }
}